package day01;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.List;

public class ActivitiDemo_02 {

    //部署方法(流程定义)
    @Test
    public void testDeploy(){

    }

    //启动流程(流程实例)
    @Test
    public void testStartProessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取运行时 RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //启动流程 (获取流程流程实例)
//        runtimeService.startProcessInstanceById();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess");

        System.out.println("流程实例ID:"+processInstance.getId());
        System.out.println("流程实例ID:"+processInstance.getProcessInstanceId());
        System.out.println("对应部署ID:"+processInstance.getDeploymentId());
        System.out.println("对应流程定义ID:"+processInstance.getProcessDefinitionId());
        System.out.println("对应流程定义Key:"+processInstance.getProcessDefinitionKey());
        System.out.println(processInstance.getName());
    }

    //查询代办任务
    @Test
    public void testQueryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取任务相关 TaskService
        TaskService taskService = processEngine.getTaskService();
        //根据负责人查询
        String userId = "李四";
        //查询都是create xxxxx Query
        //查询指定任务
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee(userId)    //根据负责人查询
                .processDefinitionKey("leaveProcess")  //根据流程定义的Key查询
                .list();
        //遍历代办任务
        for (Task task : taskList){
            System.out.println("当前任务ID:"+task.getId());
            System.out.println("当前任务名称:"+task.getName());
            System.out.println("当前任务负责人:"+task.getAssignee());
            System.out.println("当前任务创建时间:"+task.getCreateTime());
            System.out.println("当前任务对应流程实例ID:"+task.getProcessInstanceId());
            System.out.println("当前任务对应流程定义ID:"+task.getProcessDefinitionId());
        }

    }

    //任务审批 (完成任务)
    @Test
    public void testCompleteTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //任务ID
        String taskId = "17503";
        //完成任务
        taskService.complete(taskId);
    }

    //完成任务同时添加批注
    @Test
    public void testCompleteAndComment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //任务ID
        String taskId = "2505";
        //流程实例ID
        String processInstanceId = "10001";
        //添加任务批注
        taskService.addComment(taskId,processInstanceId,"允许休假");
        //完成任务
        taskService.complete(taskId);
    }

    //查询历史任务
    @Test
    public void testQueryHistoryTaskAndComment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取历史 HistoryService
        HistoryService historyService = processEngine.getHistoryService();
        //流程实例ID
        String processInstanceId = "10001";
        //查询历史批注信息必须使用 TaskService
        TaskService taskService = processEngine.getTaskService();

        List<HistoricTaskInstance> historicList = historyService.createHistoricTaskInstanceQuery()
                .processDefinitionKey("leaveProcess")  //只要请假流程的历史任务
                .processInstanceId(processInstanceId)  //过滤条件
                .finished()     ///已完成的任务
                .list();
        //遍历历史任务
        for (HistoricTaskInstance taskInstance : historicList){
            System.out.println("历史任务ID:"+taskInstance.getId());
            System.out.println("历史任务名称:"+taskInstance.getName());
            System.out.println("历史任务负责人:"+taskInstance.getAssignee());
            System.out.println("历史任务创建时间:"+taskInstance.getCreateTime());
            System.out.println("历史任务开始时间:"+taskInstance.getStartTime());
            System.out.println("历史任务结束时间:"+taskInstance.getEndTime());
            System.out.println("历史任务总耗时:"+taskInstance.getDurationInMillis()/1000/60+"分");
            System.out.println("历史任务对应流程实例ID:"+taskInstance.getProcessInstanceId());
            System.out.println("历史任务对应流程定义ID:"+taskInstance.getProcessDefinitionId());
            List<Comment> taskComments = taskService.getTaskComments(taskInstance.getId());
            //针对集合和数组操作,我们一定要判断 !== null 且长度 > 0
            if (taskComments !=null && taskComments.size() > 0){
                for (Comment taskComment : taskComments) {
                    System.out.println("当前任务的批注信息为:"+taskComment.getFullMessage());
                }
            }
            System.out.println("*****************************************");
        }
    }
}
